package com.example.demo.test.lecture.thread;

import java.util.concurrent.TimeUnit;

public class StopThread {
    volatile static Boolean stopFlag = Boolean.FALSE;

    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {
            while (true) {
                if (stopFlag) {
                    System.out.println(Thread.currentThread().getName() + "，线程被终止");
                    break;
                }
                System.out.println(Thread.currentThread().getName() + "，线程进行中");
            }
        }, "t1");
        t1.start();

        try {
            TimeUnit.MILLISECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            stopFlag = Boolean.TRUE;
            System.out.println("---已发出通知，告知t1线程停止");
        }, "t2").start();
    }
}
